package servlet;

import bean.NewsofUser;
import bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


/**
 * Servlet implementation class UserListServlet
 */
@WebServlet("/userlimit")
public class UserLimitServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLimitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer page = 1;
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
        try {
            page = Integer.parseInt(request.getParameter("p"));
        } catch (Exception e) {
        }
        Integer pageSize = 7;
        try {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        } catch (Exception e) {
        }
        Integer start = (page - 1) * pageSize;
        try {
            System.out.println(start + " " + pageSize);
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/press_release_system?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true",
                    "root", "123456");
            PreparedStatement ps = conn.prepareStatement("select * from userinfo limit ?,?");
            ps.setObject(1, start);
            ps.setObject(2, pageSize);
            ResultSet rs = ps.executeQuery();
            List<User> userlist = new ArrayList<User>();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setSex(rs.getInt("sex"));
                user.setNote(rs.getInt("note"));
                user.setPassword(rs.getString("password"));
                user.setHeadimg(rs.getString("headimg"));



                userlist.add(user);
            }

            ps = conn.prepareStatement("select count(*) from userinfo");
            rs = ps.executeQuery();
            Integer total = 0;
            if (rs.next()) {
                total = rs.getInt(1);
            }
            Integer tp = total / pageSize;
            if (total % pageSize != 0) tp++;

            StringBuilder sb = new StringBuilder();

            if (page > 1) {
                sb.append("<a href=\"../userlimit?p=1\">首页</a>&nbsp;&nbsp;");
                sb.append("<a href=\"../userlimit?p=" + (page - 1) + "\">上一页</a>&nbsp;&nbsp;");
            }

            for (int i = 1; i <= tp; i++) {
                if (i == page) {
                    sb.append("<b>" + i + "</b>&nbsp;&nbsp;");
                } else {
                    sb.append("<a href=\"../userlimit?p=" + i + "\">" + i + "</a>&nbsp;&nbsp;");
                }
            }

            if (page < tp) {
                sb.append("<a href=\"../userlimit?p=" + (page + 1) + "\">下一页</a>&nbsp;&nbsp;");
                sb.append("<a href=\"../userlimit?p=" + tp + "\">尾页</a>&nbsp;&nbsp;");
            }

            request.getSession().setAttribute("pageUser", sb.toString());
            request.getSession().setAttribute("userselectlist", userlist);
            request.getSession().setAttribute("startuser", start);

            response.sendRedirect("./jsp/UserMan.jsp"); // Redirect to news page
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
